package com.zxl.live.screen.call.ui.service;

import android.content.Intent;
import android.os.Build;
import android.os.IBinder;
import android.service.notification.NotificationListenerService;
import android.service.notification.StatusBarNotification;
import android.support.annotation.RequiresApi;

import com.zxl.live.screen.screen.ui.service.ScreenService;
import com.zxl.live.tools.tools.LogUtils;

/**
 * Created by Administrator on 2017/12/11.
 */
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
public class NotifyAccessibilityForJBMR2 extends NotificationListenerService {

//    private static NotifyAccessibilityForJBMR2 sInstance;

//    private static List<OnNotificationListener> mList = new ArrayList<>();

    @Override
    public void onCreate() {
        try {
            super.onCreate();
//            sInstance = this;
            ScreenService.startCoreService(this);
//            for (OnNotificationListener onNotificationListener : mList) {
//                onNotificationListener.onBind();
//            }
//            LogUtils.d("NotifyAccessibilityForJBMR2 onCreate");
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onDestroy() {
        try {
            super.onDestroy();
//            sInstance = null;
            LogUtils.d("NotifyAccessibilityForJBMR2 onDestroy");
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        try {
            ScreenService.startCoreService(this);
            LogUtils.d("NotifyAccessibilityForJBMR2 onStartCommand" + intent);
        } catch (Throwable e) {
            e.printStackTrace();
        }
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public IBinder onBind(Intent intent) {
        try {
            ScreenService.startCoreService(this);
            LogUtils.d("NotifyAccessibilityForJBMR2 onBind" + intent);
        } catch (Throwable e) {
            e.printStackTrace();
        }
        return super.onBind(intent);
    }

    @Override
    public void onNotificationPosted(StatusBarNotification sbn) {
        try {
            super.onNotificationPosted(sbn);
            ScreenService.startCoreService(this);
//            for (OnNotificationListener onNotificationListener : mList) {
//                onNotificationListener.onNotificationPosted(sbn);
//            }
//            LogUtils.d("onNotificationPosted : " + sbn.toString());
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onNotificationRemoved(StatusBarNotification sbn) {
        try {
            super.onNotificationRemoved(sbn);
//            for (OnNotificationListener onNotificationListener : mList) {
//                onNotificationListener.onNotificationRemoved(sbn);
//            }
//            LogUtils.d("onNotificationRemoved : " + sbn.toString());
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }

//    public static List<AppInfo> queryAllStatusBarNotification() {
//        List<AppInfo> list = null;
//        try {
//            list = new ArrayList<>();
//            if (null != sInstance) {
//                StatusBarNotification[] all = sInstance.getActiveNotifications();
//                if (null != all) {
//                    for (StatusBarNotification notification : all) {
//                        AppInfo appInfo = new AppInfo(notification);
//                        if (!appInfo.isEmpty()) {
//                            list.add(appInfo);
//                        }
//                    }
//
//                    Collections.sort(list, new Comparator<AppInfo>() {
//                        @Override
//                        public int compare(AppInfo o1, AppInfo o2) {
//                            return (int) (o2.getSbn().getPostTime() - o1.getSbn().getPostTime());
//                        }
//                    });
//                }
//            } else {
//                toggleNotificationListenerService(ApplicationHelper.getContext());
//            }
//        } catch (Throwable e) {
//            e.printStackTrace();
//        }
//        return list;
//    }
//
//    public static void clearAll() {
//        try {
//            if (null != sInstance) {
//                sInstance.cancelAllNotifications();
//            }
//        } catch (Throwable e) {
//            e.printStackTrace();
//        }
//    }
//
//    public static void register(OnNotificationListener listener) {
//        try {
//            if (!mList.contains(listener)) {
//                mList.add(listener);
//            }
//        } catch (Throwable e) {
//            e.printStackTrace();
//        }
//    }
//
//    public static void unregister(OnNotificationListener listener) {
//        try {
//            mList.remove(listener);
//        } catch (Throwable e) {
//            e.printStackTrace();
//        }
//    }
//
//    public static void clear(StatusBarNotification sbn) {
//        try {
//            if (null != sInstance) {
//                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
//                    sInstance.cancelNotification(sbn.getKey());
//                } else {
//                    sInstance.cancelNotification(sbn.getPackageName(), sbn.getTag(), sbn.getId());
//                }
//            }
//        } catch (Throwable e) {
//            e.printStackTrace();
//        }
//    }
//
//
//    public static void toggleNotificationListenerService(Context context) {
//        PackageManager pm = context.getPackageManager();
//        pm.setComponentEnabledSetting(new ComponentName(context, NotifyAccessibilityForJBMR2.class), PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
//        pm.setComponentEnabledSetting(new ComponentName(context, NotifyAccessibilityForJBMR2.class), PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
//    }
//
//    public interface OnNotificationListener {
//
//        void onBind();
//
//        void onNotificationPosted(StatusBarNotification sbn);
//
//        void onNotificationRemoved(StatusBarNotification sbn);
//
//    }

}
